/**
 * 订单变更
 * @author miaoyl
 * @date 2018/03/13
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'GWDFDOCNO', width: 160, title: '变更编号', fixed: 'left', sort: true}
            , {field: 'GWDF008', width: 100, title: '单据日期'}
            , {
                field: 'GWDF006_NAME', width: 100, title: '客户',
                search: 'default',
                searchOpts: {icon: true}, sort: true
            }
            , {
                field: 'GWDF001', width: 160, title: '分销订单号',
                search: 'default',
                searchOpts: {icon: true}, sort: true
            }
            , {
                field: 'GWDF003', width: 160, title: 'ERP订单号',
                search: 'default',
                searchOpts: {icon: true}, sort: true
            }
            , {field: 'GWDF002', width: 80, title: '版本号'}
            , {
                field: 'GWDF007', width: 100, title: '返利类型',
                filterDatas: app.datas_("crm_gwdf_gwdf007")
            }
            , {
                field: 'GWDF009', width: 80, title: '整单结束',
                filterDatas: app.datas_("crm_gwai501_gwax011")
            }
            , {field: 'GWDF032', width: 160, title: '变更原因'}
            , {field: 'GWDFACTI', width: 160, title: '状态', filterDatas: app.datas_('crm_gwcd_gwcdacti')},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwai770setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwai770SettingAction,
        moduleName = '订单变更',
        mlegal = ''
    ;

    app.newModule({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwai770',
                _searchs: [{
                    id: 'gwai770', tableId: 'gwai770_table', datas: _searchsArr.pushArray([
                        {
                            field: 'GWDFUSER', title: '申请者', search: 'default',
                            searchOpts: {icon: true}
                        },
                        {
                            field: 'GWDF033', title: '审核者', search: 'default',
                            searchOpts: {icon: true}
                        },
                        {
                            field: 'GWDFDOCDATE', title: '申请日期', search: 'default',
                            searchOpts: {type: 'daterange'}
                        },
                        {
                            field: 'GWDF034', title: '审核日期', search: 'default',
                            searchOpts: {type: 'daterange'}
                        }
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'gwai770_edit', tpl: 'gwai770_edit'
                }, {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }/*,{
                    dom: 'common_open_kh', tpl: 'common/_open_kh'
                }, {
                    dom: 'common_open_coptc', tpl: 'common/_open_coptc'
                }, {
                    dom: 'common_open_xs', tpl: 'common/_open_xs'
                }, {
                    dom: 'common_open_gwcs', tpl: 'common/_open_gwcs'
                }*/]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai770_table'),
                    url: g_ctxPath + 'crm/gwai770/getListPage',
                    exportsUrl: g_ctxPath + 'crm/gwai770/getListPage',
                    exportsName: '订单变更数据',
                    title: '订单变更信息表',
                    id: 'gwai770_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_where
                });

                layui.laytpl(app.getByid(doc, 'script-gwai770setting-action').innerHTML).render({}, function (string) {
                    gwai770SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai770_table)', function (obj) {

                    layer.tips(gwai770SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if (obj.data.GWDFACTI != 'N') {
                                            app.showWarning("只有状态为N：未审核的订单才可以编辑！");
                                            return;
                                        }
                                        self._openTplFull({
                                            tag: 'gwai770_edit',
                                            tagData: {
                                                obj: obj
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwai770_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai770_edit',
                        tagData: {
                            isCheck: true,
                            obj: obj,
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '信息编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai770_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai770_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '信息编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                        case 'check':
                            self.check();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai770_table', where: table_where});
        },

        delData: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwai770/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName + '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            var checkStatus = table.checkStatus('gwai770_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.GWDFACTI != 'N') {
                        app.showWarning("只有单据状态为N：未审核才可以删除!");
                        flag = false;
                        return;
                    }
                    var _temp = app.fields_('gwdf_t');
                    _temp.GWDFENT = v.GWDFENT;
                    _temp.GWDFDOCNO = v.GWDFDOCNO;
                    _xdata.push(_temp);
                });
                if (flag) {
                    app.ajax({
                        xurl: 'crm/gwai770/delDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: moduleName + '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        check: function () {
            var checkStatus = table.checkStatus('gwai770_table');
            var _temp = app.fields_('gwdf_t');
            if (checkStatus.data.length > 1) {
                app.showMessage("仅允许单笔操作！");
                return;
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    _temp.GWDFENT = v.GWDFENT;
                    _temp.GWDFDOCNO = v.GWDFDOCNO;
                    _xdata.push(_temp);
                });
            } else {
                app.showMessage("请先选择变更单！");
                return;
            }
            app.ajax({
                xurl: 'crm/gwai770/check',
                xdata: _temp,
                loading: true,
                cb: function (result) {
                    if (result.data.data.code == 300) {
                        self.updateNum(_temp.GWDFDOCNO, result.data.data.change_list);
                    } else if (result.data.data.code == 400) {
                        app.showMessage("订单数量小于已送货数量+准备送货数量，请调整订单变更数量!");
                        return;
                    } else if (result.data.data.code == 500) {
                        app.showMessage(result.data.message);
                        return;
                    } else {
                        self.reloadTableCurr();
                        app.showMessage("审核成功!");
                        return;
                    }
                }
            });
        },

        updateNum: function (GWDFDOCNO, list) {
            app.ajax({
                xurl: 'crm/gwai770/updateGwdf015',
                xdata: {list: list, GWDFDOCNO: GWDFDOCNO},
                loading: true,
                toJsonStr: true,
                confirm: {msg: "变更审核期间发生发货行为,是否以最新发货情况更新当前变更单?"},
                okmsg: '销货数量修改成功',
                cb: function (result) {
                    /*
                     if (result.data.data.code == 300) {
                     app.showWarning("该变更单与ERP订单数量不一致，无法审核！");
                     return ;
                     //self.reloadTableCurr();
                     }*/
                }
            });
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai770',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWDF006_NAME') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDF006_NAME = data.GWAK001;
                                            mlegal = data.GWAKPLANT;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWDF003') {//erp订单开窗
                                if(mlegal == ''){
                                    app.showWarning("请先选择客户！")
                                    return;
                                }
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_coptc',
                                            columnList: [
                                                {label: '订单编号', prop: 'TC002', width: '150'},
                                                {label: '单据日期', prop: 'TC003', width: '150'},
                                                {label: '客户编号', prop: 'TC004', width: '150'}
                                            ],
                                            searchParam:{
                                                mlegal: mlegal
                                            }
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDF003 = data.TC002;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择ERP订单',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWDFUSER') {//申请人开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysuser_id',
                                            columnList: [
                                                {label: '人员编号', prop: 'ID', width: '150'},
                                                {label: '姓名', prop: 'ACCOUNT', width: '150'},
                                                {label: '账号', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDFUSER = data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择申请人',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWDF033') {//审核人开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysuser_id',
                                            columnList: [
                                                {label: '人员编号', prop: 'ID', width: '150'},
                                                {label: '姓名', prop: 'ACCOUNT', width: '150'},
                                                {label: '账号', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDF033 = data.ACCOUNT;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择审核人',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWDF001') {//审核人开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwcsdocno',
                                            columnList: [
                                                {label: '订单单号', prop: 'GWCSDOCNO', width: '150'},
                                                {label: '订单日期', prop: 'GWCSDOCDATE', width: '150'},
                                                {label: '备注', prop: 'GWCS050', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDF001 = data.GWCSDOCNO;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择分销订单号',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    },
                    watch: {
                        'isearch.fields.GWDFDOCDATE': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.GWDFDOCDATE_START = newValue[0].replace(/-/g, '');
                                this.isearch.fields.GWDFDOCDATE_END = newValue[1].replace(/-/g, '');
                            } else {
                                this.isearch.fields.GWDFDOCDATE_START = '';
                                this.isearch.fields.GWDFDOCDATE_END = '';
                            }
                        },
                        'isearch.fields.GWDF034': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.GWDF034_START = newValue[0].replace(/-/g, '');
                                this.isearch.fields.GWDF034_END = newValue[1].replace(/-/g, '');
                            } else {
                                this.isearch.fields.GWDF034_START = '';
                                this.isearch.fields.GWDF034_END = '';
                            }
                        },
                        'isearch.fields.GWDF006_NAME': function (newValue, oldValue) {
                            if (newValue == '' || newValue == null) {
                                mlegal = '';
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },

        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});